package com.eendtech.bootpage;

import android.content.res.Resources;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.eendtech.bootpage.fragment_boot.BootOne;
import com.eendtech.bootpage.fragment_boot.BootThree;
import com.eendtech.bootpage.fragment_boot.BootTwo;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp_BootPage;
    private List<Fragment> viewList;

    //当前指示器的索引
    private int currentIndicator = 0;
    private View pont1;
    private View pont2;
    private View pont3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initIndicator();
        initViewPager();
    }
    private void initData (){
        viewList = new ArrayList<Fragment>();
        viewList.add(new BootOne());
        viewList.add(new BootTwo());
        viewList.add(new BootThree());


    }
    private void initIndicator (){
        //指示器
        int length = viewList.size();
        pont1 = findViewById(R.id.view_Ponit1);
        pont2 = findViewById(R.id.view_Ponit2);
        pont3 = findViewById(R.id.view_Ponit3);
        View [] points = {pont1,pont2,pont3};
        if(currentIndicator == 0 ){
            pont1.setBackground(getResources().getDrawable(R.drawable.radius_circle));
        }




    }

    /**
     * 初始化ViewPager
     */
    private void initViewPager (){
        //寻找布局
        vp_BootPage = findViewById(R.id.vp_BootPage);
        vp_BootPage.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return viewList.get(i);
            }

            @Override
            public int getCount() {
                return viewList.size();
            }
        });

        vp_BootPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //当界面滚动的时候
            }

            @Override
            public void onPageSelected(int position) {

               // 指示器
                switch (position){
                    case 0:
                        pont1.setBackground(getResources().getDrawable(R.drawable.radius_circle));
                        pont2.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        pont3.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        break;
                    case 1:
                        pont1.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        pont2.setBackground(getResources().getDrawable(R.drawable.radius_circle));
                        pont3.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        break;
                    case 2:
                        pont1.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        pont2.setBackground(getResources().getDrawable(R.drawable.radius_normal));
                        pont3.setBackground(getResources().getDrawable(R.drawable.radius_circle));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //当界面停止改变的时候
            }
        });


    }


}
